package com.edu.controller;

import com.edu.dao.CommentDao;

import com.edu.dao.IndexDao;
import com.edu.dao.ProductDao;
import com.edu.dao.UserDao;
import com.edu.data.Comment;
import com.edu.data.ProductCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/Guest")
public class GuestBookConroller {

    @Autowired
    CommentDao commentDao;
    @Autowired
    IndexDao dao;

    @RequestMapping("/list")
    public String getList(Model model){
        List<Comment> commentList =commentDao.getCommentList();
        model.addAttribute("comments", commentList);
        List<ProductCategory> productCategoryList = dao.getAllProductCategory();
        model.addAttribute("categories",productCategoryList);
        return "../../manage/guestbook";
    }
    @RequestMapping("/delete")
    public String delete(int id){
        commentDao.delete(id);
        return "redirect:list";
    }
    @RequestMapping("/read")
    public String read(Model model,String id) throws SQLException {
        Comment comment =commentDao.findById(Integer.parseInt(id));
        model.addAttribute("comment", comment);
        return "../../manage/guestbook-modify";
    }
    @RequestMapping("/save")
    public String save(Comment comment) throws SQLException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String date= df.format(new Date());
        comment.setEcReplyTime(date);
        commentDao.update(comment);
        return "redirect:list";
    }
    @RequestMapping("/lists")
    public String getLists(Model model){
        List<Comment> commentList =commentDao.getCommentList();
        model.addAttribute("comments", commentList);
        return "../../guestbook";
    }
    @RequestMapping("/add")
    public String add(Comment comment) throws SQLException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String date= df.format(new Date());
        comment.setEcCreateTime(date);
        commentDao.add(comment);
        return "redirect:/Guest/lists";
    }

}
